package com.damai.proxy;

import java.util.ArrayList;
import java.util.List;

import com.damai.action.factories.ApiJsonActionFactory;
import com.damai.annotation.Action;
import com.damai.annotation.Controller;
import com.damai.annotation.Crypt;
import com.damai.annotation.Token;
import com.damai.error.ApiError;

@Controller(key = "proxy_api")
@Action(value = ApiJsonActionFactory.class )
@Token
public class ProxyApi {

	@Crypt(Crypt.BOTH)
	public List<ProxyInfo> proxys(){
		
		List<ProxyInfo> list = new ArrayList<ProxyInfo>();
		
		for(ProxyAction action : ProxyAction.getProxys()){
			
			ProxyInfo info = new ProxyInfo();
			
			info.setName(action.getKey());
			info.setPosition(action.getPosition());
			info.setProxyUrl(action.getProxyUrl());
			list.add(info);
			
		}
		
		return list;
		
	}
	
	@Crypt(Crypt.BOTH)
	public Boolean config(String name,String proxyUrl,int position){
		ProxyAction action = ProxyAction.getAction(name);
		if(action==null){
			throw new ApiError("Action is not exists!");
		}
		
		action.setPosition(position);
		action.setProxyUrl(proxyUrl);
		
		return true;
	}
	
}
